package team18.yuejian.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import team18.yuejian.Model.MineModel;
import team18.yuejian.R;
import team18.yuejian.RetrofitListener;
import team18.yuejian.bean.NoteBean;
import team18.yuejian.bean.OrderBean;

public class OrderDetailActivity extends BaseActivity implements View.OnClickListener,RetrofitListener {
    private ImageView back,orderPic;
    private TextView Title,orderName,orderNum,orderPrice,orderNumber,orderTime,
                    userName,userTel,orderAddress,orderAllPrice,delate,opreation;

    private int uid,oid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        initView();
        initEvent();
        initData();
    }

    public void initView(){
        back = findViewById(R.id.Order_detail_back);
        orderPic = findViewById(R.id.Order_detail_orderPic);

        Title = findViewById(R.id.Order_detail_Title);
        orderName = findViewById(R.id.Order_detail_orderName);
        orderNum = findViewById(R.id.Order_detail_orderNum);
        orderPrice = findViewById(R.id.Order_detail_orderPrice);
        orderNumber = findViewById(R.id.Order_detail_orderNumber);
        orderTime = findViewById(R.id.Order_detail_orderTime);
        userName = findViewById(R.id.Order_detail_userName);
        userTel = findViewById(R.id.Order_detail_orderTel);
        orderAddress = findViewById(R.id.Order_detail_orderAddress);
        orderAllPrice = findViewById(R.id.Order_detail_orderAllPrice);
        opreation = findViewById(R.id.Order_detail_orderStatus);

        delate = findViewById(R.id.Order_detail_delate);
    }

    public void initEvent(){
        back.setOnClickListener(this);
        delate.setOnClickListener(this);

    }

    public void initData(){
        Title.setText("订单详情");
        Intent intent = getIntent();
        oid = intent.getIntExtra("oid",0);
        int otid = intent.getIntExtra("otid",0);
        uid = getUserId();
        MineModel mineModel = new MineModel();
        mineModel.MineGetOrderDetail(oid,otid,this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.Order_detail_back:
                    Intent intent = new Intent(OrderDetailActivity.this,MineOrderActivity.class);
                    startActivity(intent);
                break;
            case R.id.Order_detail_delate:
                    MineModel mineModel = new MineModel();
                    mineModel.MineDeleteOrder(oid,uid,this);
                break;
        }
    }


    @Override
    public void onSuccess(Object o, int flag) {
        if (flag==5){
            if (((NoteBean) o).getStatus().equals("1")){
                Intent intent = new Intent(OrderDetailActivity.this,MineOrderActivity.class);
                startActivity(intent);
            }
            Toast.makeText(this,((NoteBean) o).getMsg(),Toast.LENGTH_SHORT).show();
        }else {
            setText((OrderBean) o,flag);
        }
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
        Log.d("Order_error:",msg);
    }

    public void setText(OrderBean orderBean, int flag){
        int price,num,totatil,status;
        num = orderBean.getOnum();
        status = orderBean.getOstatus();
        if (flag==1){//活动
            price = orderBean.getAprice();
            totatil = price*num;
            orderName.setText(orderBean.getAname());
            orderPrice.setText("￥"+price);
            orderAddress.setText(""+orderBean.getAaddress());
            orderAllPrice.setText("￥"+totatil);
            orderAddress.setText("地址："+orderBean.getAaddress());
        }else if (flag==2){//课程
            price = orderBean.getCsprice();
            totatil = price*num;
            orderName.setText(orderBean.getCsname());
            orderPrice.setText("￥"+price);
            orderAllPrice.setText("￥"+totatil);
        }else if (flag==3){//比赛
            price = orderBean.getMtprice();
            totatil = price*num;
            orderName.setText(orderBean.getMtname());
            orderPrice.setText("￥"+price);
            orderAllPrice.setText("￥"+totatil);
            orderAddress.setText("地址："+orderBean.getMtaddress());
        }

        if (status==0){
            opreation.setText("已下单");
        }else if (status==1){
            opreation.setText("已处理");
        }else if (status==2){
            opreation.setText("已完成");
        }
        orderNum.setText("数量："+num);
        orderNumber.setText("订单编号："+orderBean.getOnum());
        orderTime.setText("下单时间："+orderBean.getOtime());
        userName.setText(orderBean.getUname());
        userTel.setText(orderBean.getUphone());
    }
}
